package com.example.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.HistoryRank;
import com.example.entity.News;
import com.example.mapper.HistoryRankMapper;
import com.example.qo.HistoryRankQuery;
import com.example.service.IHistoryRankService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.service.INewsService;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yunan
 * @since 2023-04-14
 */
@Service
public class HistoryRankServiceImpl extends ServiceImpl<HistoryRankMapper, HistoryRank> implements IHistoryRankService {

    @Autowired
    private INewsService newsService;

    private String prefix = "rank" + LocalDateTime.now().getMinute();

    private String prefixs;

    private static Integer flag;

    private static Integer j;

    private Double score;

    private List<Double> arr = new ArrayList();

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Resource
    private HistoryRankMapper historyRankMapper;

    @Override
    public List<News> queryPage() {
        arr.add(1.0);
        List<News> list = newsService.list(null);
        for (News new1 :list){
            int day = LocalDateTime.now().getDayOfYear() - new1.getCreateTime().getDayOfYear();
            if (day == 0) {
                score = new1.getViewnum() * 1.0;
            }else if (day == 1){
                score = new1.getViewnum() * 0.9;
            }else if (day == 2){
                score = new1.getViewnum() * 0.8;
            }else if (day >= 7){
                score = 0.0;
            }
            arr.add(score);
            redisTemplate.opsForZSet().incrementScore(prefix,new1.getId().toString(),score);
            prefixs = prefix;
        }
        Set<String> range = redisTemplate.opsForZSet().reverseRange(prefix, 0, -1);
        List<News> news = new ArrayList<>();
        for (String str : range) {
            String[] split = str.split(",");
            for (String s : split) {
                long id = Long.parseLong(s);
                News new2 = newsService.getById(id);
                news.add(new2);
            }
        }
        System.err.println(news);
        return news;
    }

    @Override
    public boolean cj() {
        flag = 0;
        Set<String> range = redisTemplate.opsForZSet().reverseRange(prefix, 0, -1);
        System.err.println(range);
        System.err.println("arr = " + arr);
        for (int i = 0; i <range.size(); i++) {
            flag = i;
        }
        int j = 0;
//        for (String str : range) {
//            String[] split = str.split(",");
//            System.err.println("split = " + split);
//            Long id = Long.parseLong(split[j]);
//            System.err.println("id = " + id);
//            if (j <= flag)
//            historyRankMapper.insert(new HistoryRank(null, prefixs, arr.get(j+1), id));
//            j++;
//        }
        for (String str : range) {
            String[] split = str.split(",");
            for (String s : split) {
                long id = Long.parseLong(s);
                if (j <= flag)
                historyRankMapper.insert(new HistoryRank(null, prefixs, arr.get(j+1), id));
                j++;
            }
        }
        return true;
    }
}
